﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="SearchView.aspx.cs" Inherits="NewsAggregator.SearchView" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Header" runat="server">
    <div data-role="header" data-theme="f">
		<h1>Search</h1>
    </div><!-- /header -->
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
    <script src="getKeywords.aspx" type="text/javascript"></script>
    <script type="text/javascript" language="javascript">
        var PageNum = 0;
        
        function NextPage() {
            Block();
            PageNum++;
            var pageUrl = '<%=ResolveUrl("~/WebService/DataService.asmx")%>';
            $.ajax({
                type: "POST",
                url: pageUrl + "/GetStoriesByKeywordSearch",
                data: "{'keyword':'" + $('#search-basic').val() + "','pageNumber':'" + PageNum + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (results) {
                    UnBlock();
                    $("#listStories").empty();
                    var items = [];
                    for (var key in results.d) {
                        items.push('<li><a onclick="UpdateHits(' + results.d[key].Id + ')" href="' + results.d[key].Link + '">' + results.d[key].Title + '</a><span class="ui-li-count">' + results.d[key].Hits + '</span><a href="#"></a></li>');
                    }
                    
                    if (PageNum == 0) {
                        $('#listStories').append(items.join(''));
                        $('#listStories').append('<li><a href="javascript:NextPage();">Next 10</a></li>');
                    } else {

                        $('#listStories').append('<li><a href="javascript:PrevPage();">Previous 10</a></li>');
                        $('#listStories').append(items.join(''));
                        $('#listStories').append('<li><a href="javascript:NextPage();">Next 10</a></li>');
                    }


                    $('#listStories').listview('refresh');
                    return false;
                }
            });
        }
        function PrevPage() {
            Block();
            PageNum--;
            var pageUrl = '<%=ResolveUrl("~/WebService/DataService.asmx")%>'
            $.ajax({
                type: "POST",
                url: pageUrl + "/GetStoriesByKeywordSearch",
                data: "{'keyword':'" + $('#search-basic').val() + "','pageNumber':'" + PageNum + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (results) {
                    UnBlock();
                    $("#listStories").empty();
                    var items = [];
                    for (var key in results.d) {
                        items.push('<li><a onclick="UpdateHits(' + results.d[key].Id + ')" href="' + results.d[key].Link + '">' + results.d[key].Title + '</a><span class="ui-li-count">' + results.d[key].Hits + '</span><a href="#"></a></li>');
                    }
                   
                    if (PageNum == 0) {
                        $('#listStories').append(items.join(''));
                        $('#listStories').append('<li><a href="javascript:NextPage();">Next 10</a></li>');
                    } else {

                        $('#listStories').append('<li><a href="javascript:PrevPage();">Previous 10</a></li>');
                        $('#listStories').append(items.join(''));
                        $('#listStories').append('<li><a href="javascript:NextPage();">Next 10</a></li>');
                    }


                    $('#listStories').listview('refresh');
                    return false;
                }
            });
        }
        
      

        $(document).bind('pageinit', function () {
            $("#search-basic").autocomplete({
                target: $('#suggestions'),
                source: keywords,
                link: "javascript:Search();return false;",
                minLength: 1
            });
            
            $("#search-basic").bind('change', function (event, ui) {
                //alert('changed');
                PageNum = 0;
                var pageUrl = '<%=ResolveUrl("~/WebService/DataService.asmx")%>';
                Block();
                $.ajax({
                    type: "POST",
                    url: pageUrl + "/GetStoriesByKeywordSearch",
                    data: "{'keyword':'" + $('#search-basic').val() + "','pageNumber':'" + PageNum + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (results) {
                        UnBlock();
                        $("#listStories").empty();
                        $("#suggestions").empty();
                        var items = [];
                        for (var key in results.d) {
                            items.push('<li><a onclick="UpdateHits(' + results.d[key].Id + ')" href="' + results.d[key].Link + '">' + results.d[key].Title + '</a><span class="ui-li-count">' + results.d[key].Hits + '</span><a href="javascript:Related(' + results.d[key].Id + ')">Related Stories</a></li>');
                        }

                        if (PageNum == 0) {
                            $('#listStories').append(items.join(''));
                            $('#listStories').append('<li><a href="javascript:NextPage();">Next 10</a></li>');
                        } else {

                            $('#listStories').append('<li><a href="javascript:PrevPage();">Previous 10</a></li>');
                            $('#listStories').append(items.join(''));
                            $('#listStories').append('<li><a href="javascript:NextPage();">Next 10</a></li>');
                        }


                        $('#listStories').listview('refresh');
                        return false;
                    }
                });
            })

            ;
           
            $("#liSearch").addClass("ui-btn-active");
            $("#liSearch").addClass("ui-state-persist");
            $("#liCategory").removeClass("ui-btn-active");
            $("#liHome").removeClass("ui-btn-active");
            //$("#search-basic").val($("#<%= hiddensearchvalue.ClientID %>").val());
        });

        function UpdateHits(newsID) {
            var pageUrl = '<%=ResolveUrl("~/WebService/RecordHitsWS.asmx")%>'
            $.ajax({
                type: "POST",
                url: pageUrl + "/UpdateHits",
                data: "{'newsid':'" + newsID + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: OnSuccessCall,
                error: OnErrorCall
            });
        }
        
        function Search() {
            //alert('changed');
            PageNum = 0;
            var pageUrl = '<%=ResolveUrl("~/WebService/DataService.asmx")%>';
                Block();
                $.ajax({
                    type: "POST",
                    url: pageUrl + "/GetStoriesByKeywordSearch",
                    data: "{'keyword':'" + $('#search-basic').val() + "','pageNumber':'" + PageNum + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (results) {
                        UnBlock();
                        $("#listStories").empty();
                        $("#suggestions").empty();
                        var items = [];
                        for (var key in results.d) {
                            items.push('<li><a onclick="UpdateHits(' + results.d[key].Id + ')" href="' + results.d[key].Link + '">' + results.d[key].Title + '</a><span class="ui-li-count">' + results.d[key].Hits + '</span><a href="javascript:Related(' + results.d[key].Id + ')">Related Stories</a></li>');
                        }

                        if (PageNum == 1) {
                            $('#listStories').append(items.join(''));
                            $('#listStories').append('<li><a href="javascript:NextPage();">Next 10</a></li>');
                        } else {

                            $('#listStories').append('<li><a href="javascript:PrevPage();">Previous 10</a></li>');
                            $('#listStories').append(items.join(''));
                            $('#listStories').append('<li><a href="javascript:NextPage();">Next 10</a></li>');
                        }


                        $('#listStories').listview('refresh');
                        return false;
                    }
                });
        }

        function OnSuccessCall(response) {

        }

        function OnErrorCall(response) {
            //alert("Error changing the status");
        }
        function Related(storyID) {
            window.location = "Related.aspx?story=" + storyID;
        }

    </script>
      <div data-role="content">
            <asp:HiddenField ID="hiddensearchvalue" runat="server" Value=""></asp:HiddenField>
            <input type="search" name="search" id="search-basic" value="" />
          <ul id="suggestions" data-role="listview" data-inset="true">
              </ul>
            <ul id="listStories" data-role="listview" data-inset="true">
                <asp:Repeater ID="srchStroriesRepeater" runat="server">
                    <itemtemplate>
                        <li>
                            <a onclick="UpdateHits('<%# DataBinder.Eval(Container.DataItem, "ID") %>')" href="<%# DataBinder.Eval(Container.DataItem, "Link") %>"><%# DataBinder.Eval(Container.DataItem, "Title") %></a>
                            <span class="ui-li-count"><%# DataBinder.Eval(Container.DataItem, "Hits") %></span>
                           <a href="javascript:Related('<%# DataBinder.Eval(Container.DataItem, "ID") %>')">Related Stories</a>
                        </li>                           
                    </itemtemplate>
                <FooterTemplate>
                    <li><a href="javascript:NextPage();">Next 10</a></li>
                </FooterTemplate>
            </asp:Repeater>       
			 </ul>
	  </div><!-- /content -->
</asp:Content>
